package ee.subjects.frontend.controller.command.employeeCommands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ee.subjects.backend.model.data.Employee;
import ee.subjects.backend.model.data.Employee_role_type;
import ee.subjects.backend.model.data.Enterprise;
import ee.subjects.backend.model.data.Person;
import ee.subjects.backend.services.EmployeeService;
import ee.subjects.backend.services.EmployeeServiceImpl;
import ee.subjects.backend.services.EnterpriseService;
import ee.subjects.backend.services.EnterpriseServiceImpl;
import ee.subjects.backend.services.PersonService;
import ee.subjects.backend.services.PersonServiceImpl;
import ee.subjects.frontend.controller.command.Command;
import ee.subjects.frontend.controller.model.CustomEmployee;
import ee.subjects.frontend.controller.model.CustomEnterpriseRelations;

public class InsertEmployeeCommand implements Command {

	@Override
	public int execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Enterprise enterprise = new Enterprise();

		if (request.getParameter("id") != null){
			int enterpriseId=Integer.parseInt(request.getParameter("id"));
			EnterpriseService enterpriseService = new EnterpriseServiceImpl();
			enterprise = enterpriseService.getEnterpriseById(enterpriseId);
			
			List<CustomEmployee> customEmployeeList = new ArrayList<CustomEmployee>();
			
			List<Employee> enterpriseEmployeeList = enterpriseService.getAllEmployeesEnterpriseHas(enterpriseId);
			
			for(Employee employee :enterpriseEmployeeList){
				CustomEmployee customEmployee = new CustomEmployee();
				customEmployee.setEnterpriseId(enterpriseId);
				customEmployee.setPersonId(employee.getPerson_fk());
				customEmployee.setEmployeeId(employee.getEmployee());
				
				// Get person full name
				PersonService PersonService = new PersonServiceImpl();
				Person person = PersonService.getPersonById(employee.getPerson_fk());
				String personFullName = person.getFirst_name()+" "+person.getLast_name();
				customEmployee.setPersonFullName(personFullName);
				customEmployeeList.add(customEmployee);
			}
			request.setAttribute ("EmployeeList", customEmployeeList);
		}
		
		request.setAttribute ("Enterprise", enterprise);
		
		
		
		return 0;
	}

}
